package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.domain.DataResult;
import com.dongdongshop.em.DataResultEnum;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.TbSpecificationOptionService;
import com.dongdongshop.service.TbSpecificationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("SpecificationController")
public class SpecificationController {

    @Reference
    private TbSpecificationService tbSpecificationService;
    @Reference
    private TbSpecificationOptionService tbSpecificationOptionService;

    @RequestMapping("specification")
    public String ToShow(){
        return "admin/specification";
    }


    @RequestMapping("selectAll")
    @ResponseBody
    public DataResult selectAll(TbSpecification tbSpecification){
        List<TbSpecification> list =  tbSpecificationService.select(tbSpecification);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Boolean insert(String optionsListJson,TbSpecification specNames){
        List<TbSpecificationOption> list = JSONObject.parseArray(optionsListJson,TbSpecificationOption.class);
        Boolean b = tbSpecificationService.insertinto(list,specNames);
        return b;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Boolean delete(Long id){
        Boolean b = tbSpecificationService.delsete(id);
        return b;
    }

    @ResponseBody
    @RequestMapping("updata")
    public List<Object> updata(Long id){
        TbSpecification stringObjectMap = tbSpecificationService.selectByid(id);
        List<TbSpecificationOption> tbSpecificationOption = tbSpecificationOptionService.seletctById(id);
        List<Object> list = new ArrayList<>();
        list.add(stringObjectMap);
        list.add(tbSpecificationOption);
        return list;
    }


    @ResponseBody
    @RequestMapping("updatas")
    public Boolean updatas(TbSpecification tbSpecification,String tbSpecificationJson){
        List<TbSpecificationOption> list = JSONObject.parseArray(tbSpecificationJson,TbSpecificationOption.class);
        Boolean b = tbSpecificationService.update(tbSpecification,list);
        return b;
    }
}
